/* Name: Supernova
 * Author: Cameron Hoerig
 * Date: 5-22-12
 * Description: The Supernova class is a random event that can occur. If a supernova is created, it destroys 
 *  any planets with which it collides. Supernova has a specified lifetime - after its creation, the object 
 *  is destroyed once its lifetime is surpassed.
 * Revisions: See Subversion logs at http://code.google.com/p/software-engineering-2012/source/list
 */

import java.awt.Graphics2D;
import java.util.LinkedList;

public class Supernova extends Item {
	private static LinkedList<Supernova> idList = new LinkedList<Supernova>();
	private Sprite sprite; // holds the sprite for the object
	private float scale;	// scale of the sprite
	private int SPRITE_RADIUS;
	private float rot = 0;	// initial rotation of the object 
	private float rotSpeed = 0; // rotation speed of the object
	private final static int lifetime = 120; // lifetime, in frames, of the object
	private int stepOn;
	public static int num = 0; // current number of instances of Supernova
	
	// Creates a supernova at the given location
	public Supernova(int px, int py) {
		Game.create((Item)this, 20); // Needed for update, draw, and destroy
		colType = COL_CIRCLE; // Needed for collision checking
		idList.add(this); // Needed for collision checking
		sprite = Sprite.load("/sprites/Supernova/supernova.png");
		SPRITE_RADIUS = sprite.getHeight()/2;
		scale = 0.0f;
		r = (int)(SPRITE_RADIUS*scale);
		x = px;
		y = py;
		stepOn = 0;
		rot = Game.randomFloat((float) (2.0f*Math.PI)); // set the initial rotation of the object
		r = (int) (SPRITE_RADIUS*scale);	// assign the radius of the object
		if (Game.randomInt(2) == 0) { // determine a random rotation speed for the object
			rotSpeed = (float) (Game.randomFloat((float) (2.0f*Math.PI/80.0f), (float) (2.0f*Math.PI/20.0f)));
		} else {
			rotSpeed = (float) (Game.randomFloat((float) (-2.0f*Math.PI/40.0f), (float) (-2.0f*Math.PI/80.0f)));
		}
		num++; // increment the current number of Supernova objects in existence
		Sound.effect(Sound.EFFECT_SUPERNOVA);
		Sound.effect(Sound.EFFECT_ALARM);
		ViewControl.centerOn(px, py);
	}
	
	// Checks for collisions, and makes grow, etc.
	public void update() {
		if (Planet.checkCollision(this, true) > 0) { // check collisions with planets
			Planet.calculateDisFromEarth();
		}
		if (stepOn++ >= lifetime) {
			Game.delete(this);
		}
		
		rot += rotSpeed; // Rotates the supernova
		scale = (float) Math.log(1.0f+(float)stepOn/lifetime);
		r = (int)(SPRITE_RADIUS*scale);
	}
	
	public void collision(Item other) {}
	
	// Gets rid of the supernova
	public void destroy() {
		idList.remove(this); // Needed for collision checking
		num--;
	}
	
	// Called every time a new frame is draw - draws the supernova on screen
	public void draw() { // draw the object
		Graphics2D gh = Game.getGraphicsHandle();
		sprite.draw(gh, x, y, rot, scale);
	}
	
	// Function needed in each item class for collision checking
	public static int checkCollision(Item other, Boolean notify) {
		int num = 0;
		for (int i = 0; i < idList.size(); i++) { // Checks all objects
			Item idOn = (Item)idList.get(i);
			if (idOn != other) {
				if (Item.intersection(other, idOn)) {
					if (notify) {
						idOn.collision(other);
					}
					num++;
				}
			}
		}
		return num;
	}
}
